/* **************************************************
 * User Group Manager
 *      author:     Baskin Tapkan - baskint@hotmail.com
 *          written for Code Plex
 *              April - 2008
 *                  http://tapkan.com
 * ****************************************************
 */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace UserGroupManager
{
    public partial class DeleteGroup : Form
    {
        string computerName = string.Empty;
        Dictionary<string, string> groupDic = new Dictionary<string, string>();

        public DeleteGroup(string systemName)
        {
            computerName = systemName;
            InitializeComponent();
            this.Text = string.Format("Delete Group for {0}", computerName);
        }

        private void DeleteGroup_Load(object sender, EventArgs e)
        {
            GroupMgr gm = new GroupMgr();
            gm.ComputerName = computerName;

            groupDic = gm.GetGroupsAndDescriptions();

            if (groupDic.Count > 0)
            {
                foreach (KeyValuePair<string, string> kvp in groupDic)
                {
                    cmbGroupName.Items.Add(kvp.Key);
                }
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cmbGroupName_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtDescription.Text = GetDescription(cmbGroupName.Text);
            btnDelete.Enabled = true;
        }

        private string GetDescription(string grpName)
        {
            string retvalue = string.Empty;
            foreach (KeyValuePair<string, string> kvp in groupDic)
            {
                if (kvp.Key.Equals(grpName))
                    return kvp.Value;
            }
            return retvalue;
        }

        private void RemoveFromList(string grpName)
        {
            groupDic.Remove(grpName);
            cmbGroupName.Items.Remove(grpName);
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            string confMsg = string.Format("Do you really want to delete group {0}?", cmbGroupName.Text);
            DialogResult confirm = MessageBox.Show(confMsg, "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            if (confirm == DialogResult.Yes)
            {
                GroupMgr gm = new GroupMgr();
                gm.ComputerName = computerName;

                bool result = gm.DeleteGroup(cmbGroupName.Text);
                if (result)
                {
                    staGrpMgr.Items[0].Text = "Group deleted";
                    txtDescription.Text = "";
                    RemoveFromList(cmbGroupName.Text);
                }
                
            }
        }
    }
}